<template>
  <el-dialog
    :title="!dataForm.id ? '新增' : '修改'"
    :close-on-click-modal="false"
    :visible.sync="visible">
    <el-form :model="dataForm" :rules="dataRule" ref="dataForm" @keyup.enter.native="dataFormSubmit()" label-width="80px">
                          <el-form-item label="适用行业" prop="industry">
        <el-input v-model="dataForm.industry" placeholder="适用行业"></el-input>
      </el-form-item>
          <el-form-item label="名称" prop="name">
        <el-input v-model="dataForm.name" placeholder="名称"></el-input>
      </el-form-item>
          <el-form-item label="核心场景" prop="scene">
        <el-input v-model="dataForm.scene" placeholder="核心场景"></el-input>
      </el-form-item>
          <el-form-item label="排序" prop="sort">
        <el-input-number v-model="dataForm.sort" placeholder="排序"></el-input-number>
      </el-form-item>
          <el-form-item label="适用主体" prop="subject">
        <el-input v-model="dataForm.subject" placeholder="适用主体"></el-input>
      </el-form-item>
          <el-form-item label="编码" prop="code">
        <el-input v-model="dataForm.code" placeholder="编码"></el-input>
      </el-form-item>
      <el-form-item label="版本" prop="edition">
        <el-input v-model="dataForm.edition" placeholder="版本"></el-input>
      </el-form-item>
       <el-form-item label="首页路径" prop="homePage">
        <el-input v-model="dataForm.homePage" placeholder="首页路径"></el-input>
      </el-form-item>
      <el-form-item label="微信模版ID" prop="wxTmpId">
        <el-input v-model="dataForm.wxTmpId" placeholder="微信模版ID"></el-input>
      </el-form-item>
    </el-form>
    <span slot="footer" class="dialog-footer">
      <el-button @click="visible = false">取消</el-button>
      <el-button type="primary" @click="dataFormSubmit()">确定</el-button>
    </span>
  </el-dialog>
</template>

<script>
  import API from '@/api'
  export default {
    data () {
      return {
        visible: false,
        dataForm: {
          id: '',
          industry: '',
          name: '',
          scene: '',
          sort: '',
          subject: '',
          code: '',
          homePage: '',
          wxTmpId: '',
          edition: '',
        },
        dataRule: {
          industry: [
            { required: true, message: '适用行业不能为空', trigger: 'blur' }
          ],
              name: [
            { required: true, message: '名称不能为空', trigger: 'blur' }
          ],
              scene: [
            { required: true, message: '核心场景不能为空', trigger: 'blur' }
          ],
              sort: [
            { required: true, message: '排序不能为空', trigger: 'blur' }
          ],
              subject: [
            { required: true, message: '适用主体不能为空', trigger: 'blur' }
          ],
              code: [
            { required: true, message: '编码不能为空', trigger: 'blur' }
          ],
              edition: [
            { required: true, message: '版本不能为空', trigger: 'blur' }
          ],
            wxTmpId: [
            { required: true, message: '微信版本ID不能为空', trigger: 'blur' }
          ],
            homePage: [
            { required: true, message: '首页路径不能为空', trigger: 'blur' }
          ]
        }
      }
    },
    methods: {
      init (id) {
        this.dataForm.id = id
        this.visible = true
        this.$nextTick(() => {
          this.$refs['dataForm'].resetFields()
          if (this.dataForm.id) {
            API.miniproduct.info(this.dataForm.id).then(({data}) => {
              if (data && data.code === 0) {
                    this.dataForm.industry = data.miniProduct.industry
                    this.dataForm.name = data.miniProduct.name
                    this.dataForm.scene = data.miniProduct.scene
                    this.dataForm.sort = data.miniProduct.sort
                    this.dataForm.subject = data.miniProduct.subject
                    this.dataForm.code = data.miniProduct.code
                    this.dataForm.edition = data.miniProduct.edition
                    this.dataForm.wxTmpId = data.miniProduct.wxTmpId
                    this.dataForm.homePage = data.miniProduct.homePage
              }
            })
          }
        })
      },
      // 表单提交
      dataFormSubmit () {
        this.$refs['dataForm'].validate((valid) => {
          if (valid) {
            var params = {
                  'id': this.dataForm.id,
                  'industry': this.dataForm.industry || undefined,
                      'name': this.dataForm.name || undefined,
                      'scene': this.dataForm.scene || undefined,
                      'sort': this.dataForm.sort || undefined,
                      'subject': this.dataForm.subject || undefined,
                      'code': this.dataForm.code || undefined,
                      'edition': this.dataForm.edition || undefined,
                      'wxTmpId': this.dataForm.wxTmpId || undefined,
                      'homePage': this.dataForm.homePage || undefined,
                }
            var tick = !this.dataForm.id ? API.miniproduct.add(params) : API.miniproduct.update(params)
            tick.then(({data}) => {
              if (data && data.code === 0) {
                this.$message({
                  message: '操作成功',
                  type: 'success',
                  duration: 1500,
                  onClose: () => {
                    this.visible = false
                    this.$emit('refreshDataList')
                  }
                })
              } else {
                this.$message.error(data.msg)
              }
            })
          }
        })
      }
    }
  }
</script>
